package org.cqut.system.clazz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cqut.system.clazz.bo.ClassBo;
import org.cqut.system.clazz.domain.Clazz;
import org.cqut.system.clazz.mapper.ClazzMapper;
import org.cqut.system.clazz.service.ClazzService;
import org.cqut.system.clazz.vo.ClassVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author XiAoXu
 */
@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz>
        implements ClazzService {
    private final ClazzMapper clazzMapper;

    public ClazzServiceImpl(ClazzMapper clazzMapper) {
        this.clazzMapper = clazzMapper;
    }

    @Override
    public IPage<ClassVo> getClassVoList(int page, int limit, String condition) {
        IPage<ClassVo> classVoIPage = clazzMapper.selectClassVoList(new Page<>(page, limit), condition);
        List<ClassVo> records = classVoIPage.getRecords();
        List<ClassBo> boList = clazzMapper.selectClassNumber();
        List<ClassVo> target = new ArrayList<>();
        for (ClassVo record : records) {
            for (ClassBo classBo : boList) {
                if (Objects.equals(record.getClassName(), classBo.getClassName())) {
                    record.setClassNumber(classBo.getClassNumber());
                    break;
                }
            }
            target.add(record);
        }
        classVoIPage.setRecords(target);
        return classVoIPage;
    }
}




